package com.sense.sensedialprehandle.exception;

import com.sense.sensedialprehandle.enums.ResultStatusCode;
import com.sense.sensedialprehandle.pojo.CommonResponse;
import com.sense.sensedialprehandle.utils.ComResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@ControllerAdvice
public class GlobleException {
    /**
     * http请求的方法不正确
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public CommonResponse httpRequestMethodNotSupportedExceptionHandler(HttpServletRequest request,HttpRequestMethodNotSupportedException e) {
        log.error("http请求的方法不正确:【\n"+ ComResponseUtil.getExceptionDetails(request,e)+"\r\n】");
        return ComResponseUtil.getExceptionDetail(request,e)
                .setCode(ResultStatusCode.RequestMethodNotSupported.getCode())
                .setMsg(ResultStatusCode.RequestMethodNotSupported.getMsg());
    }

    @ExceptionHandler(TestException.class)
    @ResponseBody
    public CommonResponse testExceptionHandler(HttpServletRequest request,TestException e) {
        log.error("测试报错信息:【\n"+ ComResponseUtil.getExceptionDetails(request,e)+"\r\n】");
        return ComResponseUtil.getExceptionDetail(request,e);
    }

    @ExceptionHandler  //处理其他异常
    @ResponseBody
    public CommonResponse allExceptionHandler(HttpServletRequest request, Exception e){
        log.error("程序出现未知异常:【\r\n"+ComResponseUtil.getExceptionDetails(request,e)+"\r\n】");
        return ComResponseUtil.getExceptionDetail(request,e);
    }
}
